﻿using BaseData.API.Read.Applications.HospitalInformation.Command;
using BaseData.API.Read.Dtos;
using BaseData.Domain.HospitalInformation;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Menu;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.API.Read.Controllers.HospitalInformation
{
    /// <summary>
    /// 科室
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManageController : ControllerBase
    {
        IMediator mediator;
        ILogger<ManageController> logger;

        public ManageController(IMediator mediator, ILogger<ManageController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 科室显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<Departments>>> DepartmentsShow([FromQuery]DepartmentsShowCommand request)
        {
            try
            {
                logger.LogInformation("显示成功");
                return await mediator.Send(request);
            }
            catch (Exception)
            {
                logger.LogError("显示失败");
                throw;
            }
        }
        /// <summary>
        /// 上传图片方法
        /// </summary>
        /// <param name="file"></param>
        /// <returns>返回影响行数</returns>
        [HttpPost]
        public IActionResult Imgs(IFormFile file)
        {
            var img=Directory.GetCurrentDirectory()+@"/wwwroot/Img/"+file.FileName;
            using(FileStream fs=new FileStream(img,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Close();
            }
            return Ok("http://localhost:5103/Img/" + file.FileName);
        }
        /// <summary>
        /// 科室级联查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MenuItem>>> Handle()
        {
            return await mediator.Send(new DepartmentsMeunCommand());
        }
        /// <summary>
        /// 医生显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<DoctorDto>>> DoctorShow([FromQuery]DoctorShowCommand request)
        {
            try
            {
                logger.LogInformation("显示成功");
                return await mediator.Send(request);
            }
            catch (Exception)
            {
                logger.LogError("显示失败");
                throw;
            }
        }
    }
}
